<?php
namespace Admin\Controller;
use Common\Controller\PublicController;
require './qiniu/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;

class OtionController extends PublicController{
	//初次打开界面
	public function addOtion(){
		$ke = $_POST["option"];
		$va = $_POST["where"];
		$id = $_SESSION["admin_id"];
		if($va == NULL){
			$result = M("otion")->where("aid={$id}")->select();
		}else{
			$sql = "select * from think_otion where {$ke} like '%{$va}%'";
			$result = M()->query($sql);
		}
		$this->assign("result",$result);
		$this->display("otion");
	}
	//添加界面
	public function tion(){
		$this->display("addOtion");
	}
	
	//添加机构
	public function add(){
		echo "<pre>";
		//print_r($_FILES);
		echo "</pre>";
		// 用于签名的公钥和私钥
		$accessKey = '-JLvYg-7Pa1SGcLzw1qyrzSt5dqpp-7Xr5ZB_XI3';
		$secretKey = 'p0srnaa2qyYM7fS5kKp_OCUTYwiUMb-Jr8C9VXxY';
		// 初始化签权对象
		$auth = new Auth($accessKey, $secretKey);
		$bucket = 'show';
		//生成上传token
		$token = $auth->uploadToken($bucket);
		$pipeline = 'abc';
		
		//要进行转码的转码操作
		$fops = "avthumb/mp4/s/640x360/vb/1.25m";
		
		//可以对转码后的文件进行使用saveas参数自定义命名，当然也可以不指定文件会默认命名并保存在当间		
 		//$savekey = base64_urlSafeEncode('{$bucket}:6416545');
		$fops = $fops.'|saveas/'."1321";
		
		$policy = array(
				'persistentOps' => $fops,
				'persistentPipeline' => $pipeline
		);
		$uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
						
		// 构建 UploadManager 对象
		$uploadMgr = new UploadManager();
		$name =array();
		foreach ($_FILES["mien"] as $k=>$v){
			foreach ($v as $ke=>$val){
				unset($_FILES["mien"]);
				$image[$ke][$k] =$val; 
			}
		}
		

		foreach ($_FILES as $k=>$v){			
					$key = date('ymd').time().mt_rand(100,999);
					$filePath = $v['tmp_name'];
					//执行上传图片
					$uploadMgr->putFile($token, $key, $filePath);
					/* $a = strrpos($v['name'],".");
					$b = substr($v['name'],$a); */
					$name[$k] .= $key;
					if($v["var"] == "var"){
					//上传文件的本地路径
						$filePath = $v['tmp_name'];
						$uploadMgr = new UploadManager();					
						//执行上传视频
						list($ret, $err) = $uploadMgr->putFile($uptoken,$key, $filePath);
				}
		}

			 	
		$_POST["uid"] = $_SESSION["admin_id"];
		$_POST["poster"] = $name['poster'];
		$_POST["logo"] = $name["logo"];
		$_POST["vcr"] = $name["vcr"];
		$result = M("otion")->add($_POST);
		foreach ($image as $v){
			$key = date('ymd').time().mt_rand(100,999);
			$filePath = $v['tmp_name'];
			//执行上传图片
			$uploadMgr->putFile($token, $key, $filePath);
			$r = M("image")->add(array(
						"id"=>$result,
						"mien"=>$key
			));
		}
		
		if($result>0 && $r>0){
			echo "添加成功";
		}else{
			echo "添加失败";
		}

	}
	/* public function add(){
		$config = array(
        'mimes'         =>  array(), //允许上传的文件MiMe类型
        'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
        'exts'          =>  array("png"), //允许上传的文件后缀
        'autoSub'       =>  false, //自动子目录保存文件
        'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
        'rootPath'      =>  './Uploads/', //保存根路径
        'savePath'      =>  '', //保存路径
        'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
        'saveExt'       =>  '', //文件保存后缀，空则使用原后缀
        'replace'       =>  false, //存在同名是否覆盖
        'hash'          =>  true, //是否生成hash编码
        'callback'      =>  false, //检测文件是否存在回调，如果存在返回文件信息数组
        'driver'        =>  '', // 文件上传驱动
        'driverConfig'  =>  array(), // 上传驱动配置
    );
		
		$upload = new \Think\Upload($config);
		$info = $upload->upload($_FILES);
		if($info){
			$_POST["uid"] = $_SESSION["admin_id"];
			$_POST["poster"] = './Uploads/'.$info["logo1"]["savename"];
			$_POST["logo"] = './Uploads/'.$info["logo2"]["savename"];	
			$result = M("otion")->add($_POST);
			if($result){
				echo "添加成功";
			}else{
				echo "添加失败";
			}
		}else{
			echo "图片格式错误";
		}
	} */
	//修改界面
	public function saveindex(){
		$id = $_GET["id"];
		$result = M("otion")->where("id={$id}")->find();
		$mien = M("image")->where("id={$id}")->select();
		$a = count($mien);
		for ($i=0;$i<$a;$i++){	
			$mien[$i]["shu"] .=$i+3;			
		}

		$this->assign("mien",$mien);
		$this->assign("id",$id);
		$this->assign("result",$result);
		$this->display("save");
	}
	//修改机构信息
	public function saves(){
		$id = $_GET["id"];
		
		foreach ($_FILES["mien"] as $k=>$v){
			foreach ($v as $ke=>$val){
				unset($_FILES["mien"]);
				$_FILES[$ke][$k] =$val;
			}
		}
		
		// 用于签名的公钥和私钥
		$accessKey = '-JLvYg-7Pa1SGcLzw1qyrzSt5dqpp-7Xr5ZB_XI3';
		$secretKey = 'p0srnaa2qyYM7fS5kKp_OCUTYwiUMb-Jr8C9VXxY';
		// 初始化签权对象
		$auth = new Auth($accessKey, $secretKey);
		$bucket = 'show';
		//生成上传token
		$token = $auth->uploadToken($bucket);
		$uploadMgr = new UploadManager();
		$bucketMgr = new BucketManager($auth);
	    // 上传单个文件 
		$result = M("otion")->where("id={$id}")->find();
		$results = M("image")->where("id={$id}")->select();
		$string =array();
	    $nam = array();
	    $aa =array();
	    foreach ($_FILES as $k=>$v){
	    	if($v["tmp_name"] != NULL){
				
	    		$key = date('ymd').time().mt_rand(100,999);
	    		$filePath = $v['tmp_name'];
	    			
	    		//执行上传图片
	    		$uploadMgr->putFile($token, $key, $filePath);
	    		$aa[] .= $result[$k];
	    		$aa[] .= $results[$k]["mien"];
	    		
	    		$nam[$k] .= $key;
	    	}
	    }
	    foreach ($aa as $v){
	    	if($v != NULL){
	    		$err = $bucketMgr->delete($bucket, $v);
	    	}
	    }
	    
	    foreach ($nam as $k=>$val){
	    	if(!is_int($k)){
	    		$_POST[$k] =$val;
	    	}else{
	    		$mid = $results[$k]["mid"];
	    		$bb[$k] = str_replace($results[$k]["mien"],$val,$results[$k]["mien"]);
	    		//修改image数据库
	    		M("image")->where("mid={$mid}")->save(array(
	    						"mien"=>$bb[$k]
	    				));
	    	}
	    }
		//执行修改otion数据库
	    $res = M("otion")->where("id={$id}")->save($_POST);
	    	     
	    if($res){
	    	echo "成功";
	    }else{
	    	echo "失败";
	    }
		

	}
	
	//删除机构
	public function del(){
			//获取id
			
			$id=I("post.id");
			$sel = M("otion")->where("id={$id}")->find();
				
			$accessKey = '-JLvYg-7Pa1SGcLzw1qyrzSt5dqpp-7Xr5ZB_XI3';
			$secretKey = 'p0srnaa2qyYM7fS5kKp_OCUTYwiUMb-Jr8C9VXxY';
			 
			// 初始化签权对象
			$auth = new Auth($accessKey, $secretKey);
			$bucket = 'show';
			 
			//生成上传token
			$token = $auth->uploadToken($bucket);
			 
			//初始化Auth状态：
			$auth = new Auth($accessKey, $secretKey);
			
			//初始化BucketManager
			$bucketMgr = new BucketManager($auth);
			
			//你要测试的空间， 并且这个key在你空间中存在
			$bucket = 'show';
			$string = M("image")->where("id={$id}")->select();
			
			$key["logo"] = $sel["logo"];
			$key["poster"] = $sel["poster"];
			$key["vcr"] = $sel["vcr"];
			foreach ($string as $k=>$v){
				$key[$k] .=$v["mien"];
			}
			
			foreach ($key as $v){
				$err = $bucketMgr->delete($bucket, $v);
			}
			$res=M("otion")->where("id={$id}")->delete();
			$res=M("image")->where("id={$id}")->delete();
			if($res){
				unlink($sel['poster']);
				unlink($sel['logo']);
				// $this->success("删除成功");
				$data['status']=1;
				$data['msg']="删除成功";
				$this->ajaxReturn($data);
			}else{
				// $this->error("删除失败");
				$data['status']=0;
				$data['msg']="删除失败";
				$this->ajaxReturn($data);
			}		
		
	}
	
}






















